//============================================================
//write by bxl at 2021-06-30 20:40:57
//============================================================


 
using System.Collections.Generic;
using System;
using BCService.Utils;
using BCCommon;
using BCData.TMS.MallInterface.MallGoodsFreight;
using BCDto.TMS.MallInterface.MallGoodsFreight;
using BCEntity.TMS.MallInterface.MallGoodsFreight;
using BCDto.Sim.Admin.Admin;
using Dapper;
using System.Data;

namespace BCService.TMS.MallInterface.MallGoodsFreight
{
    public class MallGoodsFreightService : IMallGoodsFreightService
    {
        #region var
        private readonly IDatabaseContext databaseContext;
        private IMallGoodsFreightData mallGoodsFreightData;


        #endregion

        #region 构造函数
        public MallGoodsFreightService(IMallGoodsFreightData mallGoodsFreightData, IDatabaseContext databaseContext)
        {
            this.mallGoodsFreightData = mallGoodsFreightData;
            this.databaseContext = databaseContext;
        }
        #endregion

        #region Add
        public MallGoodsFreightDto Add(MallGoodsFreightRequestDto mallGoodsFreightRequestDto, AdminDto admin)
        {
            var entity =  mallGoodsFreightRequestDto.As<MallGoodsFreightEntity>();
            entity.CreateTime = DateTime.Now;
            return mallGoodsFreightData.InsertAsync(entity).GetAwaiter().GetResult().As<MallGoodsFreightDto>();
        }
        #endregion

        #region update
        public MallGoodsFreightDto Update(MallGoodsFreightDto mallGoodsFreightDto, AdminDto admin)
        {
            var entity = mallGoodsFreightDto.As<MallGoodsFreightEntity>();
            entity.UpdateTime = DateTime.Now;
            return mallGoodsFreightData.UpdateAsync(entity).GetAwaiter().GetResult().As<MallGoodsFreightDto>();

        }
        #endregion

        #region Delete
        public void Delete(long mallGoodsFreightId)
        {
            mallGoodsFreightData.DeleteAsync(mallGoodsFreightId).GetAwaiter().GetResult();

        }
        #endregion

        #region GetByMallGoodsFreightId
        public MallGoodsFreightDto GetById(long mallGoodsFreightId)
        {
            var a = mallGoodsFreightData.GetEntityAsync(mallGoodsFreightId).GetAwaiter().GetResult();
            return a.As<MallGoodsFreightDto>();
        }
        #endregion

        #region GetList  获取商城商品运费记录--[分页]
        public PageableList<MallGoodsFreightDto> GetList(string mallShopNo, string goodsSource, string transportBusinessType, string freightType, string goodsSku, string goodsName, decimal? goodsWeight, decimal? goodsVolume, decimal? goodsLength, decimal? goodsWidth, decimal? goodsHeight, int? goodsCount, decimal? goodsPrice, string goodsUnit, bool? isMonthPay, decimal? startPrice, int? startMileage, decimal? everyMileagePrice, string provinceCode, string cityCode, string countyCode, int pageIndex, int pageSize)
        {
            int fCount = 0;
            IEnumerable<MallGoodsFreightEntity> fList;



            (fList, fCount) = mallGoodsFreightData.GetListAsync(mallShopNo,  goodsSource,  transportBusinessType,  freightType,  goodsSku,  goodsName,  goodsWeight,  goodsVolume,  goodsLength,  goodsWidth,  goodsHeight,  goodsCount,  goodsPrice,  goodsUnit,  isMonthPay,  startPrice, startMileage,  everyMileagePrice,  provinceCode,  cityCode,  countyCode,pageIndex, pageSize).GetAwaiter().GetResult();

            return new PageableList<MallGoodsFreightDto>()
            {
                Count = fCount,
                Items = fList.As<IEnumerable<MallGoodsFreightDto>>(),
                PageIndex = pageIndex,
                PageSize = pageSize,
                ServerTime = DateTime.Now,
            };
        }
        #endregion
    }
}